<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/7/4
     * Time: 下午3:29
     */

    namespace app\payment\model;


    use think\Db;
    use think\exception\ErrorException;
    use think\Model;
    use Tool\Utils\UtilEncryption;

    class PaymentOrderModel extends Model
    {

        protected $name = 'payment_pay';

        // 自动写入时间戳
        protected $autoWriteTimestamp = true;

        protected $type = ['create_time' => 'timestamp:Y-m-d H:i','update_time' => 'timestamp:Y-m-d H:i'];

        /**
         * 未付款
         */
        const ORDER_NOT_PAY  = 0;//未付款
        const ORDER_YES_PAY  = 1;//已付款
        const ORDER_CALLBACK = 2;//已回调
        const ORDER_IS_CHECK = 3;//已对账

        static public function CreatePayOrder($data)
        {

            $order_sn         = UtilEncryption::MakeSn();
            $pay_sn           = UtilEncryption::MakeSn();
            $data['order_sn'] = $order_sn;
            $data['pay_sn']   = $pay_sn;
            $data['is_ok']    = self::create([
                'member_id' => $data['member_id'],
                'order_sn'  => $order_sn,
                'pay_sn'    => $pay_sn,
                'money'     => $data['money'],
                'type'      => $data['pay_type'],
                'status'    => self::ORDER_NOT_PAY,

            ]);
            return $data;
        }

        /**
         * 修改订单状态
         *
         * @param string $pay_sn
         * @param int    $order_status
         *
         * @return PaymentOrderModel|bool
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        static public function Status($pay_sn = '',$order_status = 0)
        {

            if ($order_status == self::ORDER_CALLBACK)
            {
                try
                {
                    Db::startTrans();

                    $order         = self::where(['order_sn' => $pay_sn])->find();
                    $order->status = $order_status;
                    $order->save();

                    WalletModel::where('member_id',$order->member_id)->setInc('money',$order->money);

                    Db::commit();
                    return true;
                }
                catch (ErrorException $e)
                {
                    Db::rollback();
                    return false;
                }
            }
            else
            {
                return self::update(['status' => $order_status],['order_sn' => $pay_sn]);
            }


        }

        static public function getObjectData()
        {

            return Db::view('payment_pay',true)//字段
            ->view('member','phone','member.id = payment_pay.member_id','left');
        }

        /**
         * 数据统计
         *
         * @param $data_list
         * @param $map
         *
         * @return array
         */
        static public function data_count($map)
        {

            unset($map['status']);
            $data['status0'] = static::where($map)->where(['status' => 0])->sum('(money /100)');
            $data['status1'] = static::where($map)->where(['status' => 1])->sum('(money /100)');
            $data['alipay']  = static::where($map)->where(['status' => 1,'type' => 1])->sum('(money /100)');

            return $data;
        }

    }